package GradeManager.StudentGradeSystem.controller;

import GradeManager.StudentGradeSystem.pojo.PageBean;
import GradeManager.StudentGradeSystem.pojo.Result;
import GradeManager.StudentGradeSystem.pojo.Teacher;
import GradeManager.StudentGradeSystem.service.TeacherService;
import GradeManager.StudentGradeSystem.common.CheckGradeIegal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/teacher")
public class TeacherConterllor {
    @Autowired
    private TeacherService service;
    @GetMapping
    public Result tableinit(int currentPage,int pageSize){
        PageBean<Teacher> pageBean = service.initTable(currentPage, pageSize);
        return Result.success(pageBean);
    }
    @PostMapping("/selectByCondition")
    public Result selectbycondotion(@RequestBody Teacher teacherCondition,int currentPage,int pageSize){
        PageBean<Teacher> pageBean = service.selectByCondition(teacherCondition, currentPage, pageSize);
        return Result.success(pageBean);
    }
    @PostMapping("/addstudentgrade")
    public Result addstudentgrade(@RequestBody Teacher addform){
        String msg = CheckGradeIegal.checkGrades(addform);
        if(msg.equals("Iegal")){
            boolean addsuccess = service.addStudentGrades(addform);
            if(addsuccess){
                return Result.success();
            }else return Result.error("此学年的学生成绩已添加！");
        }else return Result.error(msg);
    }
    @PutMapping("/updateStudentGrades")
    public Result updatestudentgrades(@RequestBody Teacher updatedData){
        String msg= CheckGradeIegal.checkGrades(updatedData);
        if(msg.equals("Iegal")){
            service.updateStudentGrades(updatedData);
            return Result.success();
        }else return Result.error(msg);
    }
    @DeleteMapping("/deleteStudentGrade")
    public Result deletestudentgrade(String schoolyear,Integer studentId){
        service.deleteStudentGrade(schoolyear,studentId);
        return Result.success();
    }
    @DeleteMapping("/deletebyIds")
    public Result multidelete(@RequestBody List<Teacher> list){
        service.mutildelete(list);
        return Result.success();
    }
}
